package com.tuanzi.loan.web.bean.wechat;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.entity.wechat.LoanWechatCustomerMessage;
import com.tuanzi.loan.business.service.wechat.WechatCustomerMessageService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.vo.request.wechat.WechatCustomerMessageRequest;
import com.tuanzi.loan.vo.response.wechat.WechatCustomerMessageResponse;
import com.tuanzi.loan.web.configuration.JSFListingSessionBean;
import com.tuanzi.loan.web.lazy.wetchat.WechatCustomerMessageLazyDataModel;
import com.tuanzi.server.loan_base.constant.weixin.LoanWechatCustomerMessageStatus;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.faces.bean.ViewScoped;
import java.util.List;
import java.util.Objects;

import static com.tuanzi.loan.business.constants.DictionaryCode.*;

/**
 * @Author: chenweijun
 * @Date: 2018/9/27 17:19
 * @Description: 微信公众号客服消息Bean
 */
@Data
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class WechatCustomerMessageBean extends JSFListingSessionBean<WechatCustomerMessageRequest, WechatCustomerMessageResponse> {

    @Autowired
    private WechatCustomerMessageService wechatCustomerMessageService;

    private LoanWechatCustomerMessage loanWechatCustomerMessage;

    @Override
    protected String page() {
        return "/views/wechat/wechat-customer-message.xhtml";
    }

    @Override
    protected void initLazyDataModel() {
        $ = new WechatCustomerMessageRequest();
        lazyDataModel = new WechatCustomerMessageLazyDataModel($, session, wechatCustomerMessageService);
    }

    public String onAdd() {
        return redirect("/views/wechat/wechat-customer-message-manage.xhtml");
    }


    public String onViewWechatCustomerMessage(Long id){
        return redirect("/views/wechat/wechat-customer-message-manage.xhtml?id=" + id);
    }

    public void onLine(Long id){
        if(Objects.isNull(id)){
            addErrorMessage("请选择客服消息");
        }
        loanWechatCustomerMessage = wechatCustomerMessageService.findOne(id);

        if(Objects.isNull(loanWechatCustomerMessage)){
            addErrorMessage("客服消息不存在");
        }
        loanWechatCustomerMessage.setStatus(LoanWechatCustomerMessageStatus.ONLINE.ordinal());
        wechatCustomerMessageService.saveOrUpdate(loanWechatCustomerMessage);
        addSuccessfullyMessage();
    }


    public void offLine(Long id){
        if(Objects.isNull(id)){
            addErrorMessage("请选择客服消息");
        }

        loanWechatCustomerMessage = wechatCustomerMessageService.findOne(id);
        if(Objects.isNull(loanWechatCustomerMessage)){
            addErrorMessage("客服消息不存在");
        }

        loanWechatCustomerMessage.setStatus(LoanWechatCustomerMessageStatus.OFFLINE.ordinal());
        wechatCustomerMessageService.saveOrUpdate(loanWechatCustomerMessage);
        addSuccessfullyMessage();

    }

    @Override
    protected List<String> getDictionaryCodes() {
        return Lists.newArrayList(WECHAT_OFFICIAL_ACCOUNT, WECHAT_CUSTOMER_MESSAGEA_STATUS, WECHAT_CUSTOMER_MESSAGE_MSG_TYPE);
    }
}
